package com.ep.autoload;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class TestAuload {
	private static ClassLoader cl;
	public void autoLoad(String uri){
		reload();
		try {
			Class<?> clazz = cl.loadClass(uri);
			System.out.println(clazz);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 将更新的文件，重新加载到类加载器中
	 */
	public static void reload(){
		try {
			String rootUrl = System.getProperty("webRoot");//项目部署的绝对路径
			System.out.println("file:autoload/");
			URL[] externalURLs = new URL[]{new URL("file:autoload/")};
			cl = new URLClassLoader(externalURLs,Thread.currentThread().getContextClassLoader());
			URL url=new URL("file:autoload/");
			URL[] eURLs=new URL[]{url};
			new URLClassLoader(eURLs,Thread.currentThread().getContextClassLoader());
			File file=new File("");
			URL url2 = file.toURI().toURL();
			new URLClassLoader(new URL[]{url2});
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
}
